home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / lib / atonum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-18  |  1.3 KB  |  55 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: atonum.c,v 5.1 1993/01/19 04:46:21 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1993 USENET Community Trust
  8.  *******************************************************************************
  9.  * Bug reports, patches, comments, suggestions should be sent to:
  10.  *
  11.  *    Syd Weinstein, Elm Coordinator
  12.  *    elm@DSI.COM            dsinc!elm
  13.  *
  14.  *******************************************************************************
  15.  * $Log: atonum.c,v $
  16.  * Revision 5.1  1993/01/19  04:46:21  syd
  17.  * Initial Checkin
  18.  *
  19.  *
  20.  ******************************************************************************/
  21.  
  22. #include <ctype.h>
  23.  
  24. /*
  25.  * This is similar to atoi(), but it complains if the string
  26.  * contains any non-numeric characters.  Returns the numeric
  27.  * value on success, -1 on error.
  28.  */
  29. int atonum(str)
  30. register char *str;
  31. {
  32.     register int value;
  33.  
  34.     if (*str == '\0')
  35.     return -1;
  36.     value = 0;
  37.     while (isdigit(*str))
  38.     value = (value*10) + (*str++ - '0');
  39.     return (*str == '\0' ? value : -1);
  40. }
  41.  
  42.  
  43. #ifdef _TEST
  44. #include <stdio.h>
  45. main()
  46. {
  47.     char buf[1024];
  48.     while (gets(buf) != NULL)
  49.         printf("atonum(%s) = %d\n", buf, atonum(buf));
  50.     putchar('\n');
  51.     exit(0);
  52. }
  53. #endif
  54.  
  55.